package com.jaiky.imagespickers.preview;

import com.jaiky.imagespickers.*;
import com.jaiky.imagespickers.container.ImagePageAdapter;
import com.jaiky.imagespickers.utils.ResUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;

public class MultiImgShowAbility extends Ability {
    protected ImagePicker imagePicker;
    protected ArrayList<ImageItem> mImageItems;      //跳转进ImagePreviewFragment的图片文件夹
    protected int mCurrentPosition = 0;              //跳转进ImagePreviewFragment时的序号，第几个图片
    protected Text mTitleCount;                  //显示当前图片的位置  例如  5/31
    protected ArrayList<ImageItem> selectedImages;   //所有已经选中的图片
    protected PageSlider mViewPager;
    protected ImagePageAdapter pro;
    protected boolean isFromItems = false;

    @Override
    protected void onStart(Intent intent) {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_show_img_viewpager);
        mCurrentPosition = intent.getIntParam(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, 0);
        isFromItems = intent.getBooleanParam(ImagePicker.EXTRA_FROM_ITEMS, false);
        if (isFromItems) {
            // 据说这样会导致大量图片崩溃
            mImageItems = getIntent().getSerializableParam(ImagePicker.EXTRA_IMAGE_ITEMS);
        } else {
            // 下面采用弱引用会导致预览崩溃
            mImageItems = (ArrayList<ImageItem>) DataHolder.getInstance().retrieve(DataHolder.DH_CURRENT_IMAGE_FOLDER_ITEMS);
        }
        imagePicker = ImagePicker.getInstance();
        selectedImages = imagePicker.getSelectedImages();
        mTitleCount = (Text) findComponentById(ResourceTable.Id_showimg_text);
        mViewPager = (PageSlider) findComponentById(ResourceTable.Id_show_img_viewPager);
        pro = new ImagePageAdapter(this, mImageItems);
        pro.setPhotoViewClickListener((view, v, v1) -> onImageSingleTap());
        mViewPager.setProvider(pro);
        mViewPager.setCurrentPage(mCurrentPosition, true);
        //初始化当前页面的状态
        mTitleCount.setText(ResUtil.getString(this, ResourceTable.String_ip_preview_image_count, mCurrentPosition + 1, mImageItems.size()));
        mViewPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int position) {
                mCurrentPosition = position;
                mTitleCount.setText(ResUtil.getString(getContext(), ResourceTable.String_ip_preview_image_count
                        , mCurrentPosition + 1, mImageItems.size()));
            }
        });
    }

    /**
     * 单击时，隐藏头和尾
     */
    public void onImageSingleTap() {
        terminateAbility();
    }

}
